import { defineStore } from 'pinia'
import * as roleApi from '@/api/role'

export const useRoleStore = defineStore('role', {
  state: () => ({
    // 角色列表
    roleList: [],
    // 总记录数
    total: 0,
    // 加载状态
    loading: false,
    // 错误信息
    error: null
  }),

  getters: {
    // 获取角色列表
    getRoles: (state) => state.roleList,
    // 获取总数
    getTotal: (state) => state.total,
    // 获取加载状态
    isLoading: (state) => state.loading,
    // 获取角色选项（用于下拉选择）
    getRoleOptions: (state) => {
      return state.roleList.map(role => ({
        id: role.id,
        label: role.name,
        value: role.id
      }))
    }
  },

  actions: {
    /**
     * 加载角色列表
     * @param {Object} params 查询参数
     */
    async loadRoleList(params = {}) {
      this.loading = true
      this.error = null
      try {
        const response = await roleApi.getRoleList(params)
        if (response.code === 1) {
          this.roleList = response.data.rows || []
          this.total = response.data.total || 0
        }
      } catch (error) {
        this.error = error.message
      } finally {
        this.loading = false
      }
    },

    /**
     * 获取所有角色
     */
    async loadAllRoles() {
      this.loading = true
      this.error = null
      try {
        const response = await roleApi.getAllRoles()
        if (response.code === 1) {
          this.roleList = response.data || []
        }
      } catch (error) {
        this.error = error.message
      } finally {
        this.loading = false
      }
    },

    /**
     * 添加角色
     * @param {Object} roleData 角色数据
     * @returns {Promise} 添加结果
     */
    async addRole(roleData) {
      this.loading = true
      this.error = null
      try {
        const response = await roleApi.addRole(roleData)
        if (response.code === 1) {
          // 添加成功后重新加载列表
          await this.loadRoleList()
          return { success: true }
        }
        return { success: false, message: response.msg }
      } catch (error) {
        this.error = error.message
        return { success: false, message: error.message }
      } finally {
        this.loading = false
      }
    },

    /**
     * 更新角色
     * @param {Object} roleData 角色数据
     * @returns {Promise} 更新结果
     */
    async updateRole(roleData) {
      this.loading = true
      this.error = null
      try {
        const response = await roleApi.updateRole(roleData)
        if (response.code === 1) {
          // 更新成功后重新加载列表
          await this.loadRoleList()
          return { success: true }
        }
        return { success: false, message: response.msg }
      } catch (error) {
        this.error = error.message
        return { success: false, message: error.message }
      } finally {
        this.loading = false
      }
    },

    /**
     * 删除角色
     * @param {number} id 角色ID
     * @returns {Promise} 删除结果
     */
    async deleteRole(id) {
      this.loading = true
      this.error = null
      try {
        const response = await roleApi.deleteRole(id)
        if (response.code === 1) {
          // 删除成功后重新加载列表
          await this.loadRoleList()
          return { success: true }
        }
        return { success: false, message: response.msg }
      } catch (error) {
        this.error = error.message
        return { success: false, message: error.message }
      } finally {
        this.loading = false
      }
    },

    /**
     * 重置状态
     */
    resetState() {
      this.roleList = []
      this.total = 0
      this.loading = false
      this.error = null
    }
  }
})